package com.qingcheng.controller;

import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.pojo.goods.Pref;
import com.qingcheng.pojo.goods.TbPref;
import com.qingcheng.service.PrefService;
import com.qingcheng.service.TbPrefService;
import com.qingcheng.util.PageUtil;
import com.qingcheng.util.YouthR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/pref")
@Api(tags = "TbPrefController")
public class PrefController {

    @Autowired
    private PrefService prefService;

    @Resource
    private TbPrefService tbPrefService;

    @ApiOperation(value = "根据id获取信息")
    @GetMapping("/detail/{id}")
    public YouthR detail(@PathVariable Integer id) {

        TbPref tbPref = tbPrefService.findById(id);
        return YouthR.ok().data("tbPref",tbPref);
    }

    @ApiOperation(value = "获取信息（list不分页）")
    @GetMapping("/list")
    public YouthR list() {
        List<TbPref>  list = tbPrefService.queryAll();
        return YouthR.ok().data("list",list);
    }



    @ApiOperation(value = "获取信息（list分页）不带条件")
    @GetMapping("/listPages")
    public YouthR listPages(Integer pageNum,Integer pageSize) {
        PageUtil<TbPref> pageUtils = tbPrefService.findListAll(pageNum, pageSize);

        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "获取信息（list分页）带条件")
    @PostMapping("pageQueryByAll/{pageNum}/{pageSize}")
    public YouthR pageQueryByAll(@PathVariable Integer pageNum,
                                 @PathVariable Integer pageSize,
                                 @RequestBody TbPref tbPref){
        PageUtil<TbPref> pageUtils = tbPrefService.pageQueryByAll(pageNum, pageSize, tbPref);
        return YouthR.ok().data("pageUtils",pageUtils);
    }

    @ApiOperation(value = "添加信息")
    @PostMapping("/addPref")
    public YouthR addPref(@RequestBody TbPref tbPref) {

        boolean b = tbPrefService.saveTbPref(tbPref);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "修改信息")
    @PutMapping("/update")
    public YouthR update(@RequestBody TbPref tbPref) {
        boolean b = tbPrefService.updateTbPref(tbPref);

        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @ApiOperation(value = "删除信息（主键）")
    @DeleteMapping("/delete/{id}")
    public YouthR delete(@PathVariable Integer id) {
        boolean b = tbPrefService.deleteById(id);
        if(b){
            return YouthR.ok();
        }
        return YouthR.error();
    }

    @GetMapping("/findAll")
    public List<Pref> findAll(){
        return prefService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<Pref> findPage(int page, int size){
        return prefService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<Pref> findList(@RequestBody Map<String,Object> searchMap){
        return prefService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<Pref> findPage(@RequestBody Map<String,Object> searchMap,int page, int size){
        return  prefService.findPage(searchMap,page,size);
    }

    @GetMapping("/findById")
    public Pref findById(Integer id){
        return prefService.findById(id);
    }


    @PostMapping("/add")
    public Result add(@RequestBody Pref pref){
        prefService.add(pref);
        return new Result(200,"添加成功",null);
    }


}
